module.exports = {
	/**
	 * 添加单条数据
	 * @url agent/toufang/kh/add 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: 'ok'
		};
		try {
			let addlogs = await vk.baseDao.add({
				dbName: "opendb-admin-log",
				dataJson: {
					type: "add",
					path:"admin/dls/kh/dytflj/add",
					edit_log: "首次添加",
					old_data: "",
					new_data: data,
					user_id: userInfo._id,
					user_name: userInfo.username,
					nick_name: userInfo.nickname,
					ip: originalParam.context.CLIENTIP,
					ua: originalParam.context.CLIENTUA
				}
			});
		} catch (err) {
			console.log("日志加入失败");
		}

		// 业务逻辑开始-----------------------------------------------------------
		// 获取前端传过来的参数
		let {
			bind_first_recharge,
			bind_first_recharge_id,
			bind_again_recharge_id,
			bind_again_recharge,
			ad_id,
			desc,
			tfurl,
			agentId,
			appid,
			appname,
			tv_id,
			tv_name,
			series,
			follow,
			gzhId,
			path,
			platform,
			domain, //用于拼接抖音投放链接：domain + "?tfid=" + res.id
			setting_agent
		} = data;
		// 参数验证开始

		//校验一下填写的appid有没有对应的秘钥存在数据库里，没有的话就添加失败
		let appinfo = await db.collection('a_appid').where({
			appid: appid
		}).get()
		if (appinfo.data.length == 0) {
			res.code = 1
			res.msg = "未找到appid对应的appsecret,请先去小程序配置--小程序管理添加"
			return res;
		}
		var appsecret = appinfo.data[0].appsecret
		tfurl = "";
		path = "player";
		platform = "douyin";
		agentId = userInfo._id;
		agent_username = userInfo.username;
		agent_nickname = userInfo.nickname;
		middleman_id = userInfo.middleman_id;
		middleman_username = userInfo.middleman_username;
		middleman_nickname = userInfo.middleman_nickname;
		gzhId = "";
		// 参数验证结束
		let dbName = "a_dy_tfurl"; // 表名
		// 执行 数据库add 命令
		res.id = await vk.baseDao.add({
			dbName,
			dataJson: {
				bind_first_recharge,
				bind_first_recharge_id,
				bind_again_recharge_id,
				bind_again_recharge,
				ad_id,
				desc,
				tfurl,
				agentId,
				agent_username,
				agent_nickname,
				middleman_id,
				middleman_username,
				middleman_nickname,
				appid,
				appname,
				appsecret,
				tv_id,
				tv_name,
				series,
				follow,
				gzhId,
				path,
				platform,
				setting_agent
			}
		});

		let queryStr = "path=player&tv_id=" + tv_id + "&series=" + series + "&invite_code=" + agentId +
			"&tfid=" + res.id + "&middleman_id=" + middleman_id;
		//参考文档：https://vkdoc.fsq.pub/client/uniCloud/plus/weixin.html#%E8%8E%B7%E5%8F%96%E5%B0%8F%E7%A8%8B%E5%BA%8Furl%E9%93%BE%E6%8E%A5
		let generateRes = await vk.openapi.weixin.urllink.generate({
			appid: appid,
			appsecret: appsecret,
			path: "pages/index/index",
			query: queryStr,
			is_expire: true,
			expire_type: 1,
			expire_interval: 30, // 有效期30天
			env_version: "release", // 要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"。默认是正式版。
		});

		let url_link_expire_time = new Date().getTime() + 2592000000; //当前时间加一个月的时间。2592000000=一个月的毫秒数
		let update_tfurl = await db.collection('a_dy_tfurl').doc(res.id).update({
			url_link: generateRes.url_link,
			url_link_expire_time: url_link_expire_time,
			tfurl: domain + "?tfid=" + res.id,
			xcxpath: "pages/index/index?path=player&tv_id=" + tv_id + "&series=" + series +
				"&invite_code=" + agentId + "&tfid=" + res.id + "&middleman_id=" + middleman_id || 0,
		})

		let adds = await db.collection('a_config').add({
			name: "RebackPercent",
			tfid: res.id,
			agentId: userInfo._id,
			// firstorder: true,
			// newuser: true,
			// douyin: 100,
			// douyin_url: "",
			// douyin_token: "",
			// kuaishou: 100,
			// uc: 100,
			// tengxun: 100,
			// baidu: 100,
			// baidu_token: "",
			// oppo: 100,
			// douyinMinPrice: 0,
			// kuaishouMinPrice: 0,
			// baiduMinPrice: 0,
			// ucMinPrice: 0,
			// tengxunMinPrice: 0,
			// oppoMinPrice: 0,
			// oppo_owner_id: "",
			// oppo_api_id: "",
			// oppo_api_key: "",

			proportion_value: 0,
			hour_backhaul: 24,
			douyin_url: "",
			douyin_token: "",
			douyinRadio: 0,
			kuaishouRadio: 0,
			tengxunRadio: 0,
			ucRadio: 0,
			baiduRadio: 0,
			oppoRadio: 0,
			douyin_ReturnRatio: 0,
			douyin_UnReturnRatio: 0,
			kuaishou_ReturnRatio: 0,
			kuaishou_UnReturnRatio: 0,
			tengxun_ReturnRatio: 0,
			tengxun_UnReturnRatio: 0,
			uc_ReturnRatio: 0,
			uc_UnReturnRatio: 0,
			baidu_ReturnRatio: 0,
			baidu_UnReturnRatio: 0,
			oppo_ReturnRatio: 0,
			oppo_UnReturnRatio: 0,
			firstorder: true,
			newuser: false,
			douyin: 100,
			douyinMinPrice: 0,
			douyinMaxPrice: 99999,
			kuaishou: 100,
			kuaishouMinPrice: 0,
			kuaishouMaxPrice: 99999,
			uc: 100,
			ucMinPrice: 0,
			baidu: 100,
			baiduMinPrice: 0,
			baiduMaxPrice: 99999,
			tengxun: 100,
			tengxunMinPrice: 0,
			tengxunMaxPrice: 99999,
			baidu_token: "",
			oppo: 100,
			oppoMinPrice: 0,
			oppoMaxPrice: 99999,
			oppo_owner_id: "",
			oppo_api_id: "",
			oppo_api_key: "",
			add_date: new Date().getTime()
		})
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}